import inquirer from 'inquirer'
import {getRepoList, getTagList} from './http.js'
import path from 'path'
import util from 'util'
import chalk   from 'chalk'
import downloadGitRepo from 'download-git-repo'

async function getRepos() {
    const repoList = await getRepoList()
    const repos = repoList.map(item => item.name)
    const {repo} = await inquirer.prompt([{
        name: 'repo',
        type: 'list',
        choices: repos,
        message: '请选择一个模板'
    }])
    return repo
}
async function getTags(repo){
    const tagList = await getTagList(repo)
    const tags = tagList.map(item=>item.name)
    const {tag} = await inquirer.prompt([{
        name:'tag',
        type:'list',
        choices:tags,
        message:'请选择一个版本'
    }])
    return tag
}
//下载
async function onDownload(name,repo,tag){
    const requestUrl = `clay-cli/${repo}${tag?'#'+tag:''}`;//创建下载地址
    const cwd = process.cwd(); //获取当前命令行选择的目录
    const targetPath = path.join(cwd,name); //模板下载所在地址
    const downloadFunc = util.promisify(downloadGitRepo);
    downloadFunc(requestUrl,targetPath);
}



export default async (name, options) => {
    const repo = await getRepos();
    const tag = await getTags(repo);
    await onDownload(name,repo,tag);
    console.log(`\r\n成功创建项目 ${chalk.cyan(name)}`);
    console.log(`\r\n  cd ${chalk.cyan(name)}`);
    console.log('  npm run dev\r\n');

}

